#include "structurewidget.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QTableView>
#include <QHeaderView>
#include <QScrollBar>
#include <QToolBox>
#include "nofocusdelegate.h"


StructureWidget::StructureWidget(QScrollArea *parent)
    : QScrollArea(parent), mFileName("")
{
    setWindowFlags(Qt::FramelessWindowHint);

    setMaximumSize((Screen::GetScreenWidth()>>1), (Screen::GetScreenHeight()));
    setMinimumSize((Screen::GetScreenWidth())>>5, (Screen::GetScreenHeight())>>4);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
    setFrameShape(QScrollArea::NoFrame);
    setWidgetResizable(true);

    table_text = new QTableWidget(this);
    table_text->setFrameShape(QTableWidget::NoFrame);
    table_text->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    table_text->horizontalHeader()->setSectionsClickable(false);
    table_text->horizontalHeader()->setStretchLastSection(true);
    table_text->setItemDelegate(new NoFocusDelegate());

    table_text->setSelectionBehavior(QAbstractItemView::SelectRows);
    table_text->setSelectionMode(QAbstractItemView::SingleSelection);

    table_text->horizontalScrollBar()->hide();

    QStringList headerText;
    headerText<<QObject::tr("控件名称");  //表头标题用QStringList来表示
    table_text->setColumnCount(headerText.count());
    table_text->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
    table_text->setColumnWidth(0, 120);
    table_text->setHorizontalHeaderLabels(headerText);
    table_text->verticalHeader()->hide();

    connect(table_text, &QTableWidget::currentCellChanged, this, &StructureWidget::on_currentCellChanged);

    setWidget(table_text);
}

StructureWidget::~StructureWidget()
{

}

void StructureWidget::on_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{
    QString current_id;
    QString previous_id;

    if (currentRow == previousRow)
        return;

    QTableWidgetItem *current = table_text->item(currentRow, currentColumn);

    if (previousRow == -1)
    {
       for (auto it = map_data[mFileName].begin(); it != map_data[mFileName].end(); it++)
       {
           if (it.value()->item_name == current)
               current_id = it.key();

           previous_id = "";
       }
    }
    else
    {
        QTableWidgetItem *previous = table_text->item(previousRow, previousColumn);
        for (auto it = map_data[mFileName].begin(); it != map_data[mFileName].end(); ++it)
        {
            if (it.value()->item_name == current)
                current_id = it.key();

            if (it.value()->item_name == previous)
                previous_id = it.key();
        }
    }

    emit  signal_list_to_graphics(current_id, previous_id);
}

void StructureWidget::slt_select_item(QString random, QString name)
{
    if (random == "")
        return;

    for (auto it = map_data[mFileName].begin(); it != map_data[mFileName].end(); it++)
    {
        if (ZERO == it.key().compare(random))
        {
            int row = it.value()->item_name->row();
            it.value()->item_name->setText(name);
            table_text->selectRow(row);
        }
    }
}

void StructureWidget::delete_item(QString id)
{
    if (id == "")
        return;

    for (auto it = map_data[mFileName].begin(); it != map_data[mFileName].end();)
    {
        if (ZERO == it.key().compare(id))
        {
           int row = it.value()->item_name->row();
           table_text->takeItem(row, 0);
           table_text->removeRow(row);

           delete it.value()->item_name;
           it.value()->item_name = NULL;

           delete it.value();
           it.value() = NULL;

           it = map_data[mFileName].erase(it);
        }
        else
        {
            it++;
        }
    }
}

//拖拽创建控件时调用
void StructureWidget::CreateStructure(QString file_name, QString struct_show_item_name, QString random)
{
    AddData(file_name, random, struct_show_item_name, 0);

    ShowStructue(file_name);
}

void StructureWidget::ClearStructure()
{
    int rowcount = table_text->rowCount();

    for (int i = 0; i < rowcount; i ++)
        table_text->takeItem(i, 0);

    table_text->setRowCount(0);
}

void StructureWidget::DeleteStructureData(QString file_name)
{
    for (auto it = map_data[file_name].begin(); it != map_data[file_name].end(); it++)
    {
        if (it.value())
        {
            delete it.value();
            it.value() = NULL;
        }
    }
    map_data[file_name].clear();
}

void StructureWidget::DeleteMainWayStructureData(QString file_name)
{
    for (auto it = map_data[file_name].begin(); it != map_data[file_name].end();)
    {
        if (it.value())
        {
            QTableWidgetItem *item_name = it.value()->item_name;

            QString name = item_name->text();
            //带有id
            if (name.contains("eta") ||
                name.contains("station") ||
                name.contains("line") ||
                name.contains("point"))
            {
                delete it.value();
                it.value() = NULL;
                it = map_data[file_name].erase(it);
            } else
            {
                it++;
            }
        }
    }
}

void StructureWidget::AddData(QString file_name, QString random, QString name, int id)
{
    PtrItemInfo ptr = new ItemInfo;
    ptr->item_name = new QTableWidgetItem;
    ptr->item_name->setFlags(ptr->item_name->flags() & ~Qt::ItemIsEditable);//设置控件类型不可修改
    ptr->item_name->setText(name);
    ptr->stid = id;
    ptr->used = false;

    map_data[file_name].insert(random, ptr);
}

void StructureWidget::SortShow(QVector<PtrItemInfo> vector)
{
    for (int i = 0; i < vector.size(); i++)
    {
        QTableWidgetItem *item_name = vector[i]->item_name;
        int rowcount = table_text->rowCount();
        item_name->setTextAlignment(Qt::AlignCenter);
        table_text->insertRow(rowcount);
        table_text->setItem(table_text->rowCount() - 1, 0, item_name);
    }
}

void StructureWidget::ShowData(QString file_name)
{
    if (file_name == "")
        return;

    QVector<PtrItemInfo> vector_data;
    QVector<PtrItemInfo> vector_data_station;
    QVector<PtrItemInfo> vector_data_point;
    QVector<PtrItemInfo> vector_data_line;
    QVector<PtrItemInfo> vector_data_eta;

    for (auto it = map_data[file_name].begin(); it != map_data[file_name].end(); it++)
    {
        if (it.key() == "")
            continue;

        QString name = it.value()->item_name->text();
        if (name.contains("station") ||
            name.contains("line") ||
            name.contains("point") ||
            name.contains("eta"))
        {
            if (name.contains("station"))
                vector_data_station.push_back(it.value());
            if (name.contains("line"))
                vector_data_line.push_back(it.value());
            if (name.contains("point"))
                vector_data_point.push_back(it.value());
            if (name.contains("eta"))
                vector_data_eta.push_back(it.value());
        } else
        {
            vector_data.push_back(it.value());
        }
    }

    //排序的时候确保id小的先显示，并且点线名分别显示
    std::sort(vector_data_station.begin(), vector_data_station.end(),
          [](PtrItemInfo ele1, PtrItemInfo ele2){return ele1->stid < ele2->stid;});
    std::sort(vector_data_point.begin(), vector_data_point.end(),
          [](PtrItemInfo ele1, PtrItemInfo ele2){return ele1->stid < ele2->stid;});
    std::sort(vector_data_line.begin(), vector_data_line.end(),
          [](PtrItemInfo ele1, PtrItemInfo ele2){return ele1->stid < ele2->stid;});
    std::sort(vector_data_eta.begin(), vector_data_eta.end(),
          [](PtrItemInfo ele1, PtrItemInfo ele2){return ele1->stid < ele2->stid;});

    SortShow(vector_data);
    SortShow(vector_data_station);
    SortShow(vector_data_point);
    SortShow(vector_data_line);
}

void StructureWidget::ShowStructue(QString file_name)
{
    SetFileName(file_name);
    ClearStructure();
    ShowData(file_name);
}

